import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/model/app_info.dart';
import 'package:flutter_eve_plugin/model/health/user_info.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/login/login.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/plot/plot.dart';
import 'package:flutter_eve_plugin/pages/main/constant.dart';
import 'package:flutter_eve_plugin/pages/ye_xiu/login/actions.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/route/main.dart';
import 'package:flutter_eve_plugin/store/base_action.dart';
import 'package:flutter_eve_plugin/utils/print_util.dart';
import 'package:redux/redux.dart';
import 'package:redux_thunk/redux_thunk.dart';

userInfo(Store store, UserInfoModel model) {
  if (model == null) return;
  store.dispatch(BaseAction(type: USER_USER_INFO, data: model));
}

accountInfo(Store store, BuildContext context, AccountInfo model) {
  if (model == null) return;
  store.dispatch(BaseAction(type: YX_LOGIN_DISPATCH, data: model));
  store.dispatch(autoLoginDispatch(context, model));
}

loginDispatch(Store store, AccountInfo info) {
  if (info == null) return;
  store.dispatch(BaseAction(type: YX_LOGIN_DISPATCH, data: info));
}

ThunkAction autoLoginDispatch(BuildContext context, AccountInfo model) {
  return (Store store) async {
    AppInfo appInfo = await PluginEve.sendMessage(PluginConstant.httpYXAppInfo,
        httpType: HttpType.POST);
    if (appInfo == null) return;
    AutoLoginReq req = AutoLoginReq(
        model.accessToken, Platform.isIOS ? 1 : 2, appInfo.versionCode);
    PluginEve.sendMessage(PluginConstant.httpYXAutoLogin,
            param: req, httpType: HttpType.POST)
        .then((data) {
      AccountInfo info = data;
      loginDispatch(store, info);
      switch (info?.userStage) {
        case 2:
          Navigator.pushNamed(context, RouteConstant.LoginBirthdayPage);
          break;
        case 3:
          {
            if (info.deviceId != 0) {
              if (info.idol == 'DAYEXIU') {
                ///大叶修
                ///调转主页
              } else if (info.idol == 'C2') {
                ///C2
                ///调转主页
                goC2HomeNative();
              } else if (info.idol == 'YEXIU') {
                ///叶修
                ///调转主页
              } else {
                Navigator.pushNamed(
                    context, RouteConstant.LoginSelectMachinePage);
              }
            } else {
              Navigator.pushNamed(
                  context, RouteConstant.LoginSelectMachinePage);
            }
          }
          break;
      }
    });
  };
}

/// 触发剧情
ThunkAction doTriggerPlot(int triggerType, String triggerCondition,
    {Function() callback}) {
  return (Store store) async {
    var plotTrigger = PlotTrigger(triggerType, triggerCondition);
    await PluginEve.sendMessage(PluginConstant.actionTriggerPlot,
        param: plotTrigger);
    logDebugPrint("callback = $callback");
    if (callback != null) {
      callback();
    }
  };
}

goBackNative() {
  PluginEve.sendMessage(PluginConstant.actionAppBack, httpType: HttpType.POST);
}
